<template>
	<view>
		<span style="color: #909399;">

  {{placeholder}}
<!--<up-textarea v-model="_value" :placeholder="placeholder" :disabled="disabled" count ></up-textarea>-->
    </span>
	</view>
</template>

<script>
import componentMinxins from '../ComponentMinxins';

export default {
	mixins: [componentMinxins],
	name: "TextInput",
	props: {
		value: {
			type: String,
			default: null
		},
		placeholder: {
			type: String,
			default: '请输入内容'
		},
		disabled: {
			type: Boolean,
			default: false
		},
		render: {
			type: Boolean,
			default: true
		},
    title: {
      type: String,
      default: null
    },
    titleItems: {
      type: String,
      default: null
    },
    ApplyDetailList: {
      type: Array,
      default: () => {
        return []
      }
    },
    itemId: {
      type: String,
      required: true
    },
	},
	data() {
		return {
      _value:''
		}
	},
	mounted() {
    let value = uni.getStorageSync('formData');
    let formItems = JSON.parse(value.formItems);
    formItems.forEach(item => {
      if (item.title == this.title) {
        uni.removeStorageSync(`${item.id}`);
      }
    })
    this.setechodata()
	},
  watch: {
    _value: {
      handler(newValue) {
        let value = uni.getStorageSync('formData');
        var formItems = JSON.parse(value.formItems);
        formItems.forEach(item => {
          if (item.title == this.title) {
            item.value = newValue;
            uni.setStorageSync(item.id, newValue);
          }else if(this.titleItems == item.title && item.title == this.titleItems){
            item.props.items.forEach(item =>{
              if (item.title == this.title) {
                item.value = newValue;
                uni.setStorageSync(item.id, newValue);
              }
            })
          }
        })
      }
    }
  },
	methods: {
    setechodata(){
      // 添加数据回显逻辑
      if (this.ApplyDetailList && this.ApplyDetailList.formData  && this.itemId)  {
        // 检查 formData 中是否存在当前 itemId 对应的字段 
        if (this.ApplyDetailList.formData.hasOwnProperty(this.itemId))  {
          // 获取对应的值
          const fieldValue = this.ApplyDetailList.formData[this.itemId]; 
          
          // 如果值是数组，取第一个元素（根据你的数据结构）
          if (Array.isArray(fieldValue)  && fieldValue.length  > 0) {
            this._value = fieldValue || ''
          } else {
            // 直接赋值 
            this._value = fieldValue || '';
          }
        }
      }
    }
	}
}
</script>

<style>

</style>
